package org.phnq.babel;

import java.io.*;
import java.net.*;
import java.util.*;
import org.phnq.babel.util.*;

/**
 * <<Class summary>>
 *
 * @author Patrick Gostovic &lt;&gt;
 * @version $Rev$
 */
public final class Processor
{
	private int nativeRef;
	
	public Processor()
	{
		this.nativeRef = initNative();
	}

	protected void finalize() throws Throwable
	{
		dispose();
	}
	
	public void dispose()
	{
		if(this.nativeRef == 0)
			return;
		
		destroyNative(this.nativeRef);
		this.nativeRef = 0;
	}
	
	public ProcessorResponse process(String cmd, Map<String,String> params) throws ProcessorException
	{
		return processNative(nativeRef, cmd, params);
	}
	
	private native int initNative();
	
	private native void destroyNative(int nativeRef);

	private native ProcessorResponse processNative(int nativeRef, String cmd, Map<String,String> params);
	
	public static class ProcessorResponse
	{
		private boolean deflate = false;
		private long clientCacheDuration = 0;
		private InputStream bodyStream;
		
		public ProcessorResponse(boolean deflate, long clientCacheDuration, int nativeInputStreamRef)
		{
			this.deflate = deflate;
			this.clientCacheDuration = clientCacheDuration;
			this.bodyStream = new IStreamWrapper(nativeInputStreamRef);
		}
		
		public boolean isDeflated()
		{
			return deflate;
		}

		public long getClientCacheDuration()
		{
			return clientCacheDuration;
		}
		
		public InputStream getBodyStream()
		{
			return bodyStream;
		}
	}
}
